package com.etc.service.impl;

import com.etc.dao.PatientMapper;
import com.etc.entity.Patient;
import com.etc.service.PatientService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public boolean existOfPhone(String phone) {
        Patient patient = new Patient();
        patient.setPhone(phone);
        List<Patient> patientList = patientMapper.findSelective(patient);
        if(!patientList.isEmpty()){
            return true;
        }

        return false;
    }

    @Override
    public boolean register(String patientName, Integer age, String phone, String password, String gender, BigDecimal money) {
        Patient patient = new Patient();
        //给就诊人设置编号，uuid
        patient.setPatientId(UUID.randomUUID().toString().replaceAll("-",""));
        patient.setPatientName(patientName);
        patient.setAge(age);
        patient.setPhone(phone);
        patient.setPassword(password);
        patient.setGender(gender);
        if(money!=null){
            patient.setMoney(money);
        }

        int i = patientMapper.addPatient(patient);
        return i>0;
    }

    @Override
    public Patient login(String phone, String password) {

        Patient patient = new Patient();
        patient.setPhone(phone);
        patient.setPassword(password);
        List<Patient> patientList = patientMapper.findSelective(patient);
        if(!patientList.isEmpty()){
            //获取登录用户，内含完整信息
            Patient loginPatient =  patientList.get(0);
            //成功登录，将登录用户添加进session
            request.getSession().setAttribute("patient",loginPatient);
            return loginPatient;
        }
        return null;
    }

    @Override
    public void logout() {
        request.getSession().removeAttribute("patient");
    }

    @Override
    public Patient getPatientBySession() {
        return (Patient) request.getSession().getAttribute("patient");
    }


    //分页搜索
    @Override
    public PageInfo<Patient> getPatientListByPage(String condition, int pageNum, int pageSize) {

        StringBuffer sb = new StringBuffer();
        //对参数进行判定并进一步处理
        if (condition != null && !"".equals(condition)) {
            sb.append("%").append(condition).append("%");
        }
        // 开始分页
        PageHelper.startPage(pageNum, pageSize);
        // 执行Select查询语句
        List<Patient> list = patientMapper.find(sb.toString());
        // 封装分页对象
        PageInfo<Patient> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int updatePatient(Patient patient) {

        return patientMapper.updatePatient(patient);
    }

    @Override
    public Patient findByid(String patientId) {
        Patient patient =patientMapper.findById(patientId);
        return patient;
    }


}
